//
//  BottomScrollView.m
//  scroll嵌套Table+悬停效果
//
//  Created by chenleping on 2018/6/21.
//  Copyright © 2018年 IAPTest. All rights reserved.
//

#import "BottomScrollView.h"

#import "MJRefresh.h"

@interface BottomScrollView()<UITableViewDelegate, UITableViewDataSource>
{
    BOOL canMove;
}
@end

@implementation BottomScrollView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(canMove) name:@"contentCanMove" object:nil];
        [self setUpChildControl];
        canMove = NO;   // 最开始的时候是不能进行滑动的
    }
    return self;
}

- (void)canMove{
    canMove = YES;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if(canMove == NO){
        [scrollView setContentOffset:CGPointMake(0, 0)];
    }
    CGFloat offsetY = scrollView.contentOffset.y;
    if (offsetY <= 0) {
        [[NSNotificationCenter defaultCenter] postNotificationName:@"OtherCanMove" object:nil];  // 告诉其他滚动视图能滚动了
        canMove = NO;   // 自己不能滑动了
    }
}

- (void)setUpChildControl{
    self.backgroundColor = [UIColor yellowColor];
    self.contentSize = CGSizeMake(2 * [UIScreen mainScreen].bounds.size.width, self.bounds.size.height);
    self.pagingEnabled = YES;
    for (NSInteger i = 0; i < 2; i ++) {
        UIView * topView = [[UIView alloc] initWithFrame:CGRectMake(i * [UIScreen mainScreen].bounds.size.width, 0, [UIScreen mainScreen].bounds.size.width, 30)];
        [self addSubview:topView];
        topView.backgroundColor = [UIColor redColor];
        UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(i * [UIScreen mainScreen].bounds.size.width, 30,self.bounds.size.width, self.bounds.size.height - 30) style:UITableViewStylePlain];
        [self addSubview:tableView];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.showsHorizontalScrollIndicator = NO;
        tableView.showsVerticalScrollIndicator = NO;
        tableView.backgroundColor = [UIColor blueColor];
    }
    
}


#pragma mark --- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * cellid = @"cellid";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellid];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
    }
    NSString * str = [NSString stringWithFormat:@"%lu", indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"我是中国人%@", str];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.didSelectedBlock) {
        self.didSelectedBlock();
    }
}
@end
